package com.example.itas;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Toast;

import com.example.classes.IP;
import com.example.classes.Notice;
import com.example.classes.NoticeContents;
import com.example.classes.Pcroomlist;
import com.example.classes.Seat;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class NoticeActivity extends Activity {
	
	 private ExpandableListView explistview;
	 private ExpandableListAdapter mAdapter;
	
	 private ArrayList<NoticeContents> noticeBox;
	 
	 private static final String TITLE = "TITLE";
	 private static final String DATE = "DATE";
	 private static final String TAG = "NoticeActivity";
	 
	 private final String PATH = "/IServer-war/webresources/entities.notice/getNotice";
	 IP ip = new IP();
	 private final String HOST = ip.ip;
	 private final int PORT = 8080;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_notice);
		
		explistview = (ExpandableListView) findViewById(R.id.expandableListView1);
		
		noticeBox = new ArrayList<NoticeContents>();
		AsyncGet ag = new AsyncGet();
		ag.execute();
		
		/*noticeBox.add(new NoticeContents("Notice 1", "2013/11/16", "Sample Content1"));
		noticeBox.add(new NoticeContents("Notice 2", "2013/11/17", "Sample Content2"));
		noticeBox.add(new NoticeContents("Notice 3", "2013/11/18", "Sample Content3"));
		noticeBox.add(new NoticeContents("Notice 4", "2013/11/19", "Sample Content4"));
		noticeBox.add(new NoticeContents("Notice 5", "2013/11/20", "Sample Content5"));*/
		
		
	}
	
	private class AsyncGet extends AsyncTask<Void, Void, List<Notice>> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

		@Override
		protected List<Notice> doInBackground(Void... params) {
			HttpHost target = new HttpHost(HOST, PORT);
			HttpClient client = new DefaultHttpClient();
			HttpGet get = new HttpGet(PATH);

			// set the MIME type for POST message body.
			get.setHeader("Content-Type", "application/json");

			HttpEntity entity = null;
			try {
				HttpResponse response = client.execute(target, get);
				Log.d(TAG, "" + response.getStatusLine().getStatusCode());
				if (response.getStatusLine().getStatusCode() != 200)
					return null;

				entity = response.getEntity();
				String str = EntityUtils.toString(entity, "utf-8");
				Log.d(TAG, str);
				List<Notice> p = JSONtoNotice(str);
				return p;
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(List<Notice> result) {
			if (result != null) {
				Toast.makeText(getApplicationContext(),
						"Success to get buildings!", Toast.LENGTH_SHORT).show();
				for (Notice p : result) {
					noticeBox.add(new NoticeContents(p.getSub(), "2013/11/20", p.getText()));
				}
				List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
			    List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
			        
			 
			    for(int i = noticeBox.size()-1; i > -1; i--){ 		
			    	Map<String, String> curGroupMap = new HashMap<String, String>();
			    	curGroupMap.put(TITLE,  noticeBox.get(i).getTitle());
			    	curGroupMap.put(DATE, noticeBox.get(i).getDate());
			    	groupData.add(curGroupMap);
			    }
			    
			    for(int i = noticeBox.size()-1; i > -1; i--){
			    List<Map<String, String>> children = new ArrayList<Map<String, String>>();
			           
			    Map<String, String> curChildMap = new HashMap<String, String>();
			    children.add(curChildMap);
			    curChildMap.put(TITLE, noticeBox.get(i).getContent());
			            
			    childData.add(children);
			    }

			        // Set up our adapter
			        mAdapter = new SimpleExpandableListAdapter(
			                NoticeActivity.this,
			                
			                groupData,
			                android.R.layout.simple_expandable_list_item_2,
			                new String[] { TITLE, DATE },
			                new int[] { android.R.id.text1, android.R.id.text2 },
			                
			                childData,
			                android.R.layout.simple_list_item_1,
			                new String[] { TITLE, DATE },
			                new int[] { android.R.id.text1, android.R.id.text2 }
			                );
			        explistview.setAdapter(mAdapter);
			} else {
				Toast.makeText(getApplicationContext(),
						"Fail to get PC Rooms!", Toast.LENGTH_SHORT).show();
			}
		}
	}
	
	private List<Notice> JSONtoNotice(String json) {
		Gson gson = new Gson();
		Type t = new TypeToken<List<Notice>>() {
		}.getType();
		List<Notice> p = gson.fromJson(json, t);
		return p;
	}

	
}